﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using C1.Win.C1Input;
using C1.Win.C1List;
using C1.Win.C1TrueDBGrid;
using PCSComProduction.WorkOrder.BO;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSUtils.Utils;

using System.Collections.Generic;
using PCSComUtils.DataContext;

namespace PCSProduction.WorkOrder
{
    public partial class BaoCaoTienDoSanXuatTheoXiNghiep : Form
    {
        const string THIS = "PCSProduction.WorkOrder.BaoCaoTienDoSanXuatTheoXiNghiep";
        public BaoCaoTienDoSanXuatTheoXiNghiep()
        {
            InitializeComponent();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (txtSaleOrder.Text.Trim() == "")
            {
                MessageBox.Show("Mã hàng không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSaleOrder.Focus();
                return;
            }
            if (txtSaleOrder.Text.Trim() != "")
            {
                if (txtSaleOrder.Tag == null)
                {
                    MessageBox.Show("Mã hàng không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtSaleOrder.Focus();
                    return;
                }
            }

            if (txtProductionLine.Text.Trim() == "")
            {
                MessageBox.Show("Xí nghiệp không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProductionLine.Focus();
                return;
            }
            if (txtProductionLine.Text.Trim() != "")
            {
                if (txtProductionLine.Tag == null)
                {
                    MessageBox.Show("Xí nghiệp không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProductionLine.Focus();
                    return;
                }
            }
            BaoCaoTienDoSanXuatMaHangBO objBO = new BaoCaoTienDoSanXuatMaHangBO();
            DataTable dtbReportData = objBO.GetReportData(Convert.ToInt32(txtSaleOrder.Tag),Convert.ToInt32(txtProductionLine.Tag));
            PCSUtils.Utils.ExecuteReport objReport = new ExecuteReport();
            objReport.showReport(dtbReportData, "TienDoSanXuatXiNghiep.xml");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void BaoCaoTienDoSanXuatTheoXiNghiep_Load(object sender, EventArgs e)
        {

        }

        private void btnSale_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnSale_Click()";
            try
            {

                DataRowView drwResult = null;
                drwResult = FormControlComponents.OpenSearchForm(SO_SaleOrderDetailTable.TABLE_NAME, SO_SaleOrderDetailTable.FGOODSCODE_FLD, txtSaleOrder.Text.Trim(), null, true);
                if (drwResult != null)
                {

                    txtSaleOrder.Text = drwResult[SO_SaleOrderDetailTable.FGOODSCODE_FLD].ToString();
                    txtSaleOrder.Tag = drwResult[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD];
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtSaleOrder_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtSaleOrder_Validating()";
            try
            {
                if (txtSaleOrder.Text.Trim() != string.Empty)
                {
                    DataRowView drwResult = null;
                    drwResult = FormControlComponents.OpenSearchForm(SO_SaleOrderDetailTable.TABLE_NAME, SO_SaleOrderDetailTable.FGOODSCODE_FLD, txtSaleOrder.Text.Trim(), null, false);
                    if (drwResult != null)
                    {
                        txtSaleOrder.Text = drwResult[SO_SaleOrderDetailTable.FGOODSCODE_FLD].ToString();
                        txtSaleOrder.Tag = drwResult[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD];
                    }
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void btnProductionLine_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = THIS + ".btnProductionLine_Click()";
            try
            {
                DataRowView drwResult = null;
                Hashtable htbCondition = new Hashtable();
                //htbCondition.Add(MST_MasterLocationTable.CCNID_FLD, SystemProperty.CCNID);

                drwResult = FormControlComponents.OpenSearchForm(PRO_ProductionLineTable.TABLE_NAME, PRO_ProductionLineTable.NAME_FLD, txtProductionLine.Text, htbCondition, true);
                if (drwResult != null)
                {
                    txtProductionLine.Text = drwResult[PRO_ProductionLineTable.NAME_FLD].ToString();
                    txtProductionLine.Tag = drwResult[PRO_ProductionLineTable.PRODUCTIONLINEID_FLD];
                }
                else
                {

                    txtProductionLine.Focus();
                    txtProductionLine.SelectAll();
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void txtProductionLine_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string METHOD_NAME = THIS + ".txtProductionLine_Validating()";
            try
            {
                if (txtSaleOrder.Text.Trim() != string.Empty)
                {
                    DataRowView drwResult = null;
                    drwResult = FormControlComponents.OpenSearchForm(PRO_ProductionLineTable.TABLE_NAME, PRO_ProductionLineTable.NAME_FLD, txtProductionLine.Text, null, false);
                    //drwResult = FormControlComponents.OpenSearchForm(SO_SaleOrderDetailTable.TABLE_NAME, SO_SaleOrderDetailTable.FGOODSCODE_FLD, txtSaleOrder.Text.Trim(), null, false);
                    if (drwResult != null)
                    {
                        txtProductionLine.Text = drwResult[PRO_ProductionLineTable.NAME_FLD].ToString();
                        txtProductionLine.Tag = drwResult[PRO_ProductionLineTable.PRODUCTIONLINEID_FLD];
                    }
                    else
                    {

                        txtProductionLine.Focus();
                        txtProductionLine.SelectAll();
                    }
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }
    }
}
